package com.starbaba.collaborate.view;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.starbaba.collaborate.model.CollaborateInfo;
import com.starbaba.collaborate.model.CollaborateResult;
import com.starbaba.collaborate.service.CollaborateService;
import com.starbaba.collaborate.service.impl.CollaborateServiceImpl;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.constant.WebConstants;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.system.user.model.UserVO;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class CollaborateAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private String fileName;
	private InputStream input;

	public String getFileName() {
		return fileName;
	}

	public InputStream getInput() {
		return input;
	}
	
	private String channel_id;
	private String show_type;
	
	public void setChannel_id(String channel_id) {
		this.channel_id = channel_id;
	}

	public void setShow_type(String show_type) {
		this.show_type = show_type;
	}

	CollaborateService collaborateService = new CollaborateServiceImpl();

	// 展示页面并且准备数据
	public String showPage() {
		UserVO user = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		collaborateService.showpage(paramsMap, user.getUserid());
		return ActionSupport.SUCCESS;
	}

	public String showCollaList() {
		UserVO user = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		collaborateService.showCollabPage(paramsMap, user.getUserid());
		return "gopage";
	}

	// 保存合作方信息
	public void saveCollaborateInof() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String channel = (String) paramsMap.get("channel");
		String username = (String) paramsMap.get("username");
		String ra = (String) paramsMap.get("ratio");

		Integer ratio = null;
		if (!StringUtils.isEmpty(ra)) {
			ratio = Integer.parseInt(ra);
		}

		CollaborateResult result = collaborateService.updateCollaborateInof(username, channel, ratio);
		try {
			sendJsonMessage(JsonUtils.toJson(result, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}
	}

	/**
	 * 合作方统计列表
	 */
	public void getCollaborateList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		UserVO user = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		// System.out.println(user.getUserid());
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		try {
			List list = collaborateService.getCollaborateList(start, length, pageResponse, paramsMap, user.getUserid());
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			LogService.error(e);
		}
	}

	public void getCollabRegisterList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		UserVO user = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		PageResponse pageResponse = new PageResponse();
		try {
			List list = collaborateService
					.getCollabRegisterList(pageRequest, pageResponse, paramsMap, user.getUserid());
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			LogService.error(e);
		}
	}

	/**
	 * 导出
	 */
	public String exportDetailCVS() throws UnsupportedEncodingException {

		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		UserVO user = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			List list = collaborateService.getCollaborateList(-1, -1, pageResponse, paramsMap, user.getUserid());
			String value = collaborateService.exportCollaborateListCVS(list);
			input = new ByteArrayInputStream(value.getBytes("GBK"));
			fileName = System.currentTimeMillis() + ".csv";
		} catch (Exception e) {
			LogService.error(e);
		}
		return ActionSupport.SUCCESS;
	}
	
	
	/**
	 * 导出
	 */
	public String exportRegisterDetailCVS() throws UnsupportedEncodingException {

		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		UserVO user = (UserVO) Struts2Utils.getSessionAttribute(WebConstants.SESSION_ATTRIBUTE_USER);
		PageResponse pageResponse = new PageResponse();
		parsePageRequest();
		pageRequest.setIDisplayStart(-1);
		pageRequest.setIDisplayLength(-1);
		try {
			List list = collaborateService
					.getCollabRegisterList(pageRequest, pageResponse, paramsMap, user.getUserid());
			String value = collaborateService.exportCollaborateRegisterListCVS(list);
			input = new ByteArrayInputStream(value.getBytes("GBK"));
			fileName = System.currentTimeMillis() + ".csv";
		} catch (Exception e) {
			LogService.error(e);
		}
		return ActionSupport.SUCCESS;
	}

	// 跳转到合作方表配置页面
	public String showTableTypePage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		collaborateService.showTableTypePage(paramsMap);
		return "tabletype";
	}

	// 获取所有合作方信息
	public void getCollaborateChannelRatioList() {
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		try {
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			List<CollaborateInfo> list = collaborateService.getCollaborateChannelRatioList(pageRequest, pageResponse, paramsMap);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			LogService.error(e);
			e.printStackTrace();
		}
	}

	// 保存表配置修改信息
	public void updatetable() {
		CollaborateResult result = collaborateService.updatetable(channel_id, show_type);
		try {
			sendJsonMessage(JsonUtils.toJson(result, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
			e.printStackTrace();
		}
	}
}
